<template>
  <el-dialog
    :title="!dataForm.projectId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="55%"
    top="5vh"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="dataForm.projectName" placeholder="项目名称" />
      </el-form-item>
      <el-form-item label="项目简介" prop="projectSynopsis">
        <quill-editor
          ref="myQuillEditor"
          v-model="dataForm.projectSynopsis"
          :options="editorOption"
          @blur="onEditorBlur($event)"
          @focus="onEditorFocus($event)"
          @change="onEditorChange($event)"
        />
      </el-form-item>
      <el-form-item label="技术点" prop="technicalPoint">
        <el-input v-model="dataForm.technicalPoint" placeholder="技术点" />
      </el-form-item>
      <el-form-item label="角色担当" prop="rolePlay">
        <el-input v-model="dataForm.rolePlay" placeholder="角色担当" />
      </el-form-item>
      <el-form-item label="难点总结" prop="difficultySummary">
        <el-input v-model="dataForm.difficultySummary" type="textarea" autosize placeholder="难点总结" />
      </el-form-item>
      <el-form-item label="个人收获" prop="personalGains">
        <el-input v-model="dataForm.personalGains" type="textarea" autosize placeholder="个人收获" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-input v-model="dataForm.startTime" placeholder="开始时间" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-input v-model="dataForm.endTime" placeholder="结束时间" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import reqApi from '@/api/project'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        projectId: '',
        projectName: '',
        projectSynopsis: '',
        technicalPoint: '',
        rolePlay: '',
        difficultySummary: '',
        personalGains: '',
        startTime: '',
        endTime: ''
      },
      editorOption: {},
      dataRule: {
        projectName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        projectSynopsis: [
          { required: true, message: '项目简介不能为空', trigger: 'blur' }
        ],
        technicalPoint: [
          { required: true, message: '技术点不能为空', trigger: 'blur' }
        ],
        rolePlay: [
          { required: true, message: '角色担当不能为空', trigger: 'blur' }
        ],
        difficultySummary: [
          { required: true, message: '难点总结不能为空', trigger: 'blur' }
        ],
        personalGains: [
          { required: true, message: '个人收获不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.projectId = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.projectId) {
          reqApi.getInfo(this.dataForm.projectId).then(({ data }) => {
            if (data) {
              this.dataForm.projectName = data.project.projectName
              this.dataForm.projectSynopsis = data.project.projectSynopsis
              this.dataForm.technicalPoint = data.project.technicalPoint
              this.dataForm.rolePlay = data.project.rolePlay
              this.dataForm.difficultySummary = data.project.difficultySummary
              this.dataForm.personalGains = data.project.personalGains
              this.dataForm.startTime = data.project.startTime
              this.dataForm.endTime = data.project.endTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.projectId) {
            reqApi.save(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            reqApi.update(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    },
    onEditorReady(editor) { // 准备编辑器

    },
    onEditorBlur() {}, // 失去焦点事件
    onEditorFocus() {}, // 获得焦点事件
    onEditorChange() {} // 内容改变事件
  }
}
</script>
<style>
.ql-editor{
    max-height: 350px;
}
</style>
